package com.example.administrator.nurse;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.nurse.base.BaseActivity;
import com.example.administrator.nurse.feature.FeatureActivity;
import com.example.administrator.nurse.model.adapter.CommonAdapter;
import com.example.administrator.nurse.model.adapter.ViewHolder;
import com.example.administrator.nurse.present.AuxiliaryToolsActivity;
import com.example.administrator.nurse.present.BodyCheckActivity;
import com.example.administrator.nurse.present.ConfigActivity;
import com.example.administrator.nurse.present.LiquorManageActivity;
import com.example.administrator.nurse.present.MoneyCheckActivity;
import com.example.administrator.nurse.present.NursingAssessmentActivity;
import com.example.administrator.nurse.present.OrderActivity;
import com.example.administrator.nurse.present.PatientRoomActivity;
import com.example.administrator.nurse.present.WorkStatisticsActivity;
import com.example.administrator.nurse.util.ViewFactory;
import com.example.administrator.nurse.util.imageLoderHelper;
import com.example.administrator.nurse.widget.CycleViewPager;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    GridView gridView;
    CycleViewPager viewPager;
    ArrayList<String> titleList;
    ArrayList<Integer> imgList;
    ArrayList<Integer> imgIdContainer;
    protected ImageLoader imageloader = ImageLoader.getInstance();

    @Override
    protected int setLayotId() {
        return R.layout.activity_main;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        f(R.id.scan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                common.jumpToCoder(MainActivity.this, null);
            }
        });
        initView();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case common.CodeScan:
                Log.d("zzzzz","data "+data.getStringExtra("content"));
                break;
                /** 个人信息页面返回 */
            case 1:
                Log.d("zzzzz","on head select back");
                imageloader.displayImage(common.nHeadUrl, ((ImageView)f(R.id.nurse_head)), imageLoderHelper.getLoderOption(R.mipmap.user, 80));
                break;
        }
    }

    @Override
    protected void onFailure(int requestCode, Object object, String errorMsg) {

    }

    @Override
    protected void onSuccess(int requestCode, Object object, String response) {

    }

    private void initView(){
        configMainHeader(MainActivity.this, getResources().getString(R.string.main_title), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ConfigActivity.class);
                startActivityForResult(intent,1);
            }
        });

        imageloader.displayImage(
                common.nHeadUrl,
                ((ImageView) f(R.id.nurse_head)),
                imageLoderHelper.getLoderOption(R.mipmap.user, 80),
                new ImageLoadingListener() {
                    @Override
                    public void onLoadingStarted(String s, View view) {

                    }

                    @Override
                    public void onLoadingFailed(String s, View view, FailReason failReason) {
                        Log.d("zzzzz",s);
                    }

                    @Override
                    public void onLoadingComplete(String s, View view, Bitmap bitmap) {

                    }

                    @Override
                    public void onLoadingCancelled(String s, View view) {
                        Log.d("zzzzz",s);
                    }
                }
        );
        ((TextView)f(R.id.nurse_name)).setText(common.nName);
        ((TextView)f(R.id.nurse_num)).setText("工号"+common.nId);

        titleList=getTileList();
        imgList=getImgList();
//        imgViewContainer=getImgViewContainer();
        imgIdContainer=getImgIdContainer();
        viewPager= (CycleViewPager) findViewById(R.id.banner);
//        viewPager.setImgList(imgViewContainer);
        viewPager.setImgList(MainActivity.this,imgIdContainer);
        viewPager.setAdapter(null);
        viewPager.setTimeout(3000);
        viewPager.addOnPageChangeListener(null);
        viewPager.moveThread.start();

        GridAdapter adapter=new GridAdapter(titleList,R.layout.main_grid_item,MainActivity.this);
        gridView= (GridView) findViewById(R.id.bottonLine);
        gridView.setAdapter(adapter);
        gridView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Log.d("nurse", "---onItemSelected---" + position);
                itemSelected(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d("nurse", "---onItemClick---" + position);
                itemSelected(position);
            }
        });
    }
    private void itemSelected(int position){
        switch (position){
            case 0:
                startOtherActivity(MainActivity.this, OrderActivity.class);      /** 医嘱执行 */
                break;
            case 1:
                startOtherActivity(MainActivity.this,FeatureActivity.class);     /** 体征录入 */
                break;
            case 2:
                startOtherActivity(MainActivity.this,PatientRoomActivity.class); /** 病房巡视 */
                break;
            case 3:
                startOtherActivity(MainActivity.this, MoneyCheckActivity.class);/** 费用查询 */
                break;
            case 4:
                startOtherActivity(MainActivity.this, BodyCheckActivity.class);/** 体检报告 */
                break;
            case 5:
                startOtherActivity(MainActivity.this, LiquorManageActivity.class);/** 配液管理 */
                break;
            case 6:
                startOtherActivity(MainActivity.this, NursingAssessmentActivity.class);/** 护理评估 */
                break;
            case 7:
                startOtherActivity(MainActivity.this, WorkStatisticsActivity.class);/** 工作统计 */
                break;
            case 8:
                startOtherActivity(MainActivity.this, AuxiliaryToolsActivity.class);/** 辅助工具 */
                break;
        }
    }
    private ArrayList<ImageView> getImgViewContainer(){
        ArrayList<ImageView> imgViewContainer=new ArrayList<ImageView>();
        imgViewContainer.add(ViewFactory.getImageView(MainActivity.this,R.mipmap.img4));
        imgViewContainer.add(ViewFactory.getImageView(MainActivity.this,R.mipmap.img2));
        imgViewContainer.add(ViewFactory.getImageView(MainActivity.this,R.mipmap.img3));
        return imgViewContainer;
    }
    private ArrayList<Integer> getImgIdContainer(){
        LinearLayout indicator= (LinearLayout) findViewById(R.id.indicator);
        ArrayList<Integer> imgIdContainer=new ArrayList<Integer>();
        imgIdContainer.add(R.mipmap.img1);
        imgIdContainer.add(R.mipmap.img2);
        imgIdContainer.add(R.mipmap.img3);
        imgIdContainer.add(R.mipmap.img4);
        imgIdContainer.add(R.mipmap.img5);
        return imgIdContainer;
    }
    private ArrayList<String> getTileList(){
        ArrayList<String> titleList=new ArrayList<>();
        Resources res=getResources();
        titleList.add(res.getString(R.string.order_title));
        titleList.add(res.getString(R.string.feature_title));
        titleList.add(res.getString(R.string.visit_title));
        titleList.add(res.getString(R.string.accouont_title));
        titleList.add(res.getString(R.string.report_title));
        titleList.add(res.getString(R.string.dispensing_title));
        titleList.add(res.getString(R.string.assessment_title));
        titleList.add(res.getString(R.string.working_title));
        titleList.add(res.getString(R.string.tools_title));
        return titleList;
    }
    private ArrayList<Integer> getImgList(){
        ArrayList<Integer> imgList=new ArrayList<>();
        imgList.add(R.mipmap.execution_of_doctors_order);
        imgList.add(R.mipmap.sign_entry);
        imgList.add(R.mipmap.ward_inspection);
        imgList.add(R.mipmap.search_cost);
        imgList.add(R.mipmap.medical_examination_report);
        imgList.add(R.mipmap.liquid_distribution_management);
        imgList.add(R.mipmap.nursing_assessment);
        imgList.add(R.mipmap.job_account);
        imgList.add(R.mipmap.assistant_tool);
        return imgList;
    }
    private boolean isExit;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK){
            if (isExit){
                finish();
            }else{
                Toast.makeText(MainActivity.this,getResources().getString(R.string.press_quit),Toast.LENGTH_LONG).show();
                isExit=true;
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        isExit=false;
                    }
                },2000);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    class GridAdapter extends CommonAdapter{

        public GridAdapter(List mDatas, int layoutId, Context context) {
            super(mDatas, layoutId, context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null){
                convertView=View.inflate(MainActivity.this,R.layout.main_grid_item,null);
            }
            TextView textView= (TextView) ViewHolder.get(R.id.main_item, convertView);
            ImageView img= (ImageView) ViewHolder.get(R.id.grid_img,convertView);
            img.setImageResource(imgList.get(position));
            textView.setText(titleList.get(position));
            return convertView;
        }
    }
}
